package com.example.demo.service;

import com.example.demo.common.domain.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public String register(User user, String Code, String Firstcode) {
        Boolean flag;
        String message = "";
        String Uname = user.getuName();
        System.out.println("执行一次查询：");
        User user1 = userMapper.selectByPrimaryKey(Uname);
        if (user1 == null) {
            message = "正确";
            flag = true;
        } else {
            message = "账户已存在，请换一个名称重新注册";
            flag = false;
        }
        if (flag) {
            if (Code.equals(Firstcode)) {
                System.out.println("对比：" + Code.equals(Firstcode));
                message = "正确";
                flag = true;
            } else {
                System.out.println("对比：" + Code.equals(Firstcode));
                message = "验证码不正确，重新输入";
                flag = false;
            }
        }
        if (flag) {
            userMapper.insertSelective(user);
        }
        return message;
    }

    public String login(User user) {
        Boolean flag;
        String message = "";
        String Uname = user.getuName();
        System.out.println(Uname);
        User user1 = userMapper.selectByPrimaryKey(Uname);
        if (user1 == null) {
            message = "账户不存在请，去注册";
            flag = false;
        } else {
            message = "正确";
            flag = true;
        }
        if (flag) {
            if (user.getuPwd().equals(user1.getuPwd())) {
                message = "正确";
            } else {
                message = "密码不正确，重新输入";
            }
        }
        return message;
    }

    public String forgetPwd(User user, String Code, String Firstcode) {

        Boolean flag;
        String message = "";
        String Uname = user.getuName();
        System.out.println("执行一次查询：");
        User user1 = userMapper.selectByPrimaryKey(Uname);
        if (user1 == null) {
            message = "账户名输入错误";
            flag = false;
        } else {
            flag = true;
        }
        if (flag) {
            if (Code.equals(Firstcode)) {
                System.out.println("对比：" + Code.equals(Firstcode));
                message = "设置密码成功，请重新登录！";
                flag = true;
            } else {
                System.out.println("对比：" + Code.equals(Firstcode));
                message = "验证码不正确，重新输入";
                flag = false;
            }
        }
        if (flag) {
            userMapper.updateByPrimaryKeySelective(user);
        }
        return message;
    }

    public User SelectUserMsg(String Uname) {
        User user = userMapper.selectByPrimaryKey(Uname);
        System.out.println(user.getuName());
        return user;
    }

    public String UpdateUserMsg(User user) {
        userMapper.updateByPrimaryKeySelective(user);
        System.out.println(user.getuName());
        return "更新成功";
    }

}
